package cn.wolfcode.crm.serivce.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.serivce.IPermissionService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper mapper;
    @Autowired
    RequestMappingHandlerMapping rmhm;
    @Override
    public void deleteByPrimaryKey(Long id) {
        mapper.deleteByPrimaryKey(id);
    }



    @Override
    public void saveOrUpdate(Permission record) {
        if (record.getId() == null) {
            //保存操作
            mapper.insert(record);
        }else {
            mapper.updateByPrimaryKey(record);
        }
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }


    @Override
    public List<Permission> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public List<Permission> selectByRoleId(Long id) {
        return mapper.selectByRoleId(id);
    }

    @Override
    public void reload() {
       //获取所有的权限
        List<String> resources = mapper.selectResources();
        //获取所有的@RequestMapping
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
        Collection<HandlerMethod> values = handlerMethods.values();
        //遍历
        for (HandlerMethod method : values) {
            RequiresPermissions annotation = method.getMethodAnnotation(RequiresPermissions.class);
            if (annotation != null) {
                String[] value = annotation.value();
                //如果不存在数据库,才添加进入
                if (!resources.contains(value[0])) {
                    Permission p = new Permission();
                    p.setResource(value[0]); //第一个value 是权限表达式
                    p.setName(value[1]); //第二个value 是权限名称
                    mapper.insert(p);
                }
            }
        }
    }

    @Override
    public List<String> selectNameByEmployeeId(Long id) {
        return mapper.selectNameByEmployeeId(id);
    }

    @Override
    public PageResult query(QueryObject qo) {
//        Integer total = mapper.count(qo);
//        if (total > 0) {
//            List<Permission> rows = mapper.query(qo);
//
//            return new PageResult(total,rows);
//        }
//        return new PageResult(0, Collections.EMPTY_LIST);
        return null;
    }
}
